#!/usr/bin/env -S node -r ./livescript-transform-implicit-async/register

require! <[
  ./CONST.ls
  ./Git.ls
  ./cache-dir.ls
  ./log
  path
  chalk
]>

require! {
  \global-npm : npm
  \fs-extra : fs
}

do !~>
  url = CONST.REPOSITORY
  dir = cache-dir.git url

  exit = []
  process.on-exit = exit.push.bind exit

  run = !~>
    try
      await require(dir)!
    catch err
      console.trace!
      log.err err

  init = ~>
    if await Git(dir).npm-install url
      await run!
    else
      return true

  if await init!
    upgrade = path.join(dir,'upgrade.ls')
    try
      upgrade = require(upgrade) dir
      await run!
      upgrade = await upgrade
      if upgrade
        await upgrade?!
    catch err
      console.trace!
      log.err err
      log.err '加载出错，尝试重新安装'
      await fs.remove(dir)
      await init!

  await Promise.all [ i! for i in exit ]
  # process.exit!


